﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ExSystem
{
    public class ExString
    {

        /// <summary>
        /// 移除最后一个匹配的字符串。
        /// </summary>
        /// <param name="source"></param>
        /// <param name="lastStr"></param>
        /// <returns></returns>
        public  static string RemoveLastString(string source, string lastStr)
        {
            
            if (!string.IsNullOrEmpty(source)&& !string.IsNullOrEmpty(lastStr))
            {
              
                return source.Substring(0, source.LastIndexOf(lastStr));
             
            }
            return source;
        }

        /// <summary>
        ///把一个字符串数组以指定分隔符合成一个字符串。
        /// </summary>
        /// <param name="arrayss"></param>
        /// <param name="seperator"></param>
        /// <returns></returns>
        public static string ToString(string[] arrayss,string seperator)
        {
            return ToString(arrayss, seperator, "", "");
           
        }

        /// <summary>
        /// 分格出一个有效数组，该数组不包含空字符串元素，并且每个元素都执行过Trim()操作。
        /// </summary>
        /// <param name="source"></param>
        /// <param name="spector"></param>
        /// <returns></returns>
        public static string[] SplitTrim(string source,string spector)
        {
            string[] a= source.Split(new string[] { spector }, StringSplitOptions.RemoveEmptyEntries);
            for (int i=0; i < a.Length; i++)
            {
                a[i] = a[i].Trim();
            }
            return a;
        }

        /// <summary>
        /// 每个用spector1分格出的元素再用spector2分格，对每个再次分格出的字符串数组中取其中的位于index位置的值组成数组返回,如果index位置的值不存在则取空串。
        /// </summary>
        /// <param name="source"></param>
        /// <param name="spector1"></param>
        /// <param name="spector2"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string[] SplitTwice(string source,string spector1,string spector2,int index,StringSplitOptions options)
        {
            string[] a = source.Split(new string[] { spector1 },options);
            for (int i=0; i<a.Length;i++ )
            {
                string data = "";
                string[] b = a[i].Split(new string[] { spector2 }, options);
                if (b.Length > index)
                {
                    data=b[index];
                }
                a[i] = data;
            }
            return a;
        }


      
        /// <summary>
        /// 把一个字符串数组以指定分隔符合成一个字符串，并且在合成时可为每个字符加前后添加字符。preStr:前面的字符;endStr:后面的字符。
        /// </summary>
        /// <param name="arrayss"></param>
        /// <param name="seperator"></param>
        /// <param name="preStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static string ToString(string[] arrayss, string seperator,string preStr,string endStr)
        {
            StringBuilder b = new StringBuilder();
            foreach (string s in arrayss)
            {
                b.Append(preStr);
                b.Append(s);
                b.Append(endStr);
                b.Append(seperator);
            }
           
            return RemoveLastString(b.ToString(), seperator);
        }

        /// <summary>
        /// 判断一个字符串数组里是否包含否一字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="needfindString"></param>
        /// <param name="comparisonType"></param>
        /// <returns></returns>
        public static bool Contains(string[] source, string needfindString, StringComparison comparisonType)
        {
            foreach (string a in source)
            {
                if (a.Equals(needfindString, comparisonType)) return true;

            }
            return false;
        }

        /// <summary>
        /// 判断一个字符串数组里是否包含否一字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="needfindString"></param>
        /// <returns></returns>
        public static bool Contains(string[] source, string needfindString)
        {
            if (source == null || source.Length <= 0) return false;
            
            foreach (string a in source)
            {
                if (a.Equals(needfindString)) return true;
            }
            return false;
        }
    }
}
